﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;

namespace DTC.Converters
{
    [ValueConversion(typeof(TimeSpan), typeof(string))]
    public class IntDaysToStringTimeSpan : IValueConverter
    {

        public IntDaysToStringTimeSpan()
        {
            tstsc = new TimeSpanToStringConverter();
        }
        private TimeSpanToStringConverter tstsc;

        public object Convert(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            if (value == null)
                return "";
            int val = (int) value;
            if (val == NoEnd)
                return "До отмены";
            return tstsc.Convert(TimeSpan.FromDays(val), targetType, parameter, culture);
        }
        public object ConvertBack(object value, Type targetType, object parameter,
            CultureInfo culture)
        {
            if (value is string)
            {
                string str = (string)value;
                str = str.ToLower();
                if (str.CompareTo("до отмены") == 0)
                    return NoEnd;

                int res = (int)((TimeSpan)tstsc.ConvertBack(value, targetType, parameter, culture)).Days;
                return res;
            }
            else return null;
        }
        public static int NoEnd = 356000;

    }
}
